package com.migu.spms.webapi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.migu.spms.dao.entity.BaseLineCriteria;
import com.migu.spms.dao.entity.BaseLineDO;
import com.migu.spms.dao.entity.ProjectCriteria;
import com.migu.spms.dao.entity.ProjectDO;
import com.migu.spms.service.data.PaginationResult;
import com.migu.spms.service.data.User;
import com.migu.spms.service.inf.IBaseLineService;
import com.migu.spms.utils.annotation.type.Method;
import com.migu.spms.utils.exception.ServiceException;
import com.migu.spms.webapi.data.BaseLineRequirementRequest;
import com.migu.spms.webapi.data.BaseLineRequirementResponse;
import com.migu.spms.webapi.data.EditRequireRequest;
import com.migu.spms.webapi.data.RequirementResponse;

@com.migu.spms.utils.annotation.type.Controller(introduction = "基线需求")
@RequestMapping(value = "/flow", produces = "application/json;charset=UTF-8")
@Controller
public class BaseLineRequirementController extends BaseController {

	@Resource(name = "baseLineServiceImpl")
	private IBaseLineService baseLineService;

	/** 展示所有未打基線版本的需求列表 */
	@Method(introduction = "基线需求", requestType = BaseLineRequirementRequest.class, responseType = BaseLineRequirementResponse.class)
	@ResponseBody
	@RequestMapping("/listUnBasedLineRequirement")
	public String listUnBasedLineRequirement(HttpServletRequest request, String sessionId) {
		User user = getUserBySession(request, sessionId);
		if (user == null) {
			throw new ServiceException("用戶未登錄！");
		}
		BaseLineCriteria req = ParseMsg(request, BaseLineCriteria.class);
		PaginationResult<BaseLineRequirementResponse> requirement = baseLineService.getAllUnBasedLineRequirement(req);
		return generateResponse(requirement, 200);
	}

	/** 打需求基线 */
	@ResponseBody
	@RequestMapping("/baseLineRequirement")
	public String baseLineRequirement(HttpServletRequest request, String sessionId) {
		User user = getUserBySession(request, sessionId);
		if (user == null) {
			throw new ServiceException("用户未登录 , 请重试 !");
		}
		BaseLineCriteria criteria = ParseMsg(request, BaseLineCriteria.class);
		baseLineService.updateBaseLineRequirement(criteria);
		return generateResponse(200);
	}

	/** 复制需求 */
	@ResponseBody
	@RequestMapping("/copyRequirement")
	public String copyRequirement(HttpServletRequest request, String sessionId) {
		User user = getUserBySession(request, sessionId);
		if (user == null) {
			throw new ServiceException("用户未登录!");
		}
		BaseLineCriteria criteria = ParseMsg(request, BaseLineCriteria.class);
		PaginationResult<RequirementResponse> copyRequirement = baseLineService.copyRequirement(criteria, user);
		return generateResponse(copyRequirement, 200);
	}

	/** 编辑需求表单回显 */
	@ResponseBody
	@RequestMapping("/requirementFormEcho")
	public String requirementFormEcho(HttpServletRequest request, String sessionId) {
		User user = getUserBySession(request, sessionId);
		if (null == user) {
			throw new ServiceException("用户未登录!");
		}
		BaseLineCriteria criteria = ParseMsg(request, BaseLineCriteria.class);
		PaginationResult<RequirementResponse> requirement = baseLineService.getRequirement(criteria);
		return generateResponse(requirement, 200);
	}

	/** 提交编辑 */
	@ResponseBody
	@RequestMapping("/submitEchoRequirement")
	public String submitEchoRequirement(HttpServletRequest request, String sessionId) throws Exception {
		User user = getUserBySession(request, sessionId);
		if (null == user) {
			throw new ServiceException("用户未登录!");
		}
//		User user = new User();
//		user.setUserId(1);
//		user.setUserName("李乐1");
		
		EditRequireRequest editRequireRequest = ParseMsg(request, EditRequireRequest.class);
		baseLineService.updateRequirement(editRequireRequest, user);
		return generateResponse(200);
	}

	@ResponseBody
	@RequestMapping("/projectList")
	public String projectList(HttpServletRequest request, String sessionId) {

		ProjectCriteria record = ParseMsg(request, ProjectCriteria.class);

		PaginationResult<ProjectDO> result = baseLineService.getAllProject(record);

		return generateResponse(result, 200);
	}

	@ResponseBody
	@RequestMapping("/queryUnBasedLineList")
	public String queryUnBasedLineList(HttpServletRequest request, String sessionId) {
		User user = getUserBySession(request, sessionId);
		if (user == null) {
			generateResponse(401);
		}
		BaseLineCriteria criteria = ParseMsg(request, BaseLineCriteria.class);
		PaginationResult<BaseLineDO> result = baseLineService.getAllUnBasedLineVersion(criteria);
		return generateResponse(result, 200);
	}
}
